package com.boilerCloud.dao.work;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.maintenance.MaintenancePlan;
import com.boilerCloud.dto.work.WorkOrder;
import com.boilerCloud.util.StringUtil;

@Repository
public class WorkOrderDao extends BaseDao {

	public void qryList(WorkOrder model, HzlqswPage<WorkOrder> page)throws Exception {
		String countsql = "select count(*) from work_order a "
				+ " left join d_device_info b on b.id = a.device_id"
				+ " left join app_user e on b.user_id = e.id" 
				+ " where a.is_delete='n' ";
			
		String listSql = "select a.*,b.name deviceName,e.name customerName "
					+ " from work_order a "
					+ " left join d_device_info b on b.id = a.device_id"
					+ " left join app_user e on b.user_id = e.id" 
					+ " where a.is_delete='n' ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(model.getOrderNo())){
			countsql += " and a.order_no like ?";
			listSql += " and a.order_no like ?";
			params.add("%" + model.getOrderNo() +"%");
		}
		
		if(!StringUtil.isEmpty(model.getDeviceId())){
			countsql += " and a.device_id = ?";
			listSql += " and a.device_id = ?";
			params.add( model.getDeviceId() );
		}
		if(!StringUtil.isEmpty(model.getId())){
			countsql += " and a.id = ?";
			listSql += " and a.id = ?";
			params.add( model.getId() );
		}
		if(!StringUtil.isEmpty(model.getDeviceName())){
			countsql += " and b.name like ?";
			listSql += " and b.name like ?";
			params.add("%" + model.getDeviceName() +"%");
		}
	
		listSql += " order by a.id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, WorkOrder.class);
		
	}

	public void save(WorkOrder model) throws Exception{
		String sql = "insert into work_order(device_id,description,contact_name,contact_phone,"
				+ "order_no,order_type,status,create_time,modify_time,is_delete,attendant,"
				+ "creator_name,creator_id,repair_id,maintenance_id) values (?,?,?,?,?,?,1,now(),now(),"
				+ "'n',?,?,?,?,?)";
		List<Object> param = new ArrayList<>();
		param.add(model.getDeviceId());
		param.add(model.getDescription());
		param.add(model.getContactName());
		param.add(model.getContactPhone());
		param.add(model.getOrderNo());
		param.add(model.getOrderType());
		param.add(model.getAttendant());
		param.add(model.getCreatorName());
		param.add(model.getCreatorId());
		param.add(model.getRepairId());
		param.add(model.getMaintenanceId());
		this.jdbcTemplate.update(sql,param.toArray());
		
	}

}
